package com.realbrand.net.realbrandpay.ui.activity.wallet.order;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.adapter.ViewPagerAdapter;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.bean.OrderBean;
import com.realbrand.net.realbrandpay.bean.OrderFragmentBean;
import com.realbrand.net.realbrandpay.ui.activity.wallet.WalletActivity;
import com.realbrand.net.realbrandpay.ui.activity.wallet.personer.adress.ManagerAdressActivity;
import com.realbrand.net.realbrandpay.untils.Constant;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/24.
 */

public class OrderActivity extends BaseActivity {


    @BindView(R.id.allOrder_toolbar)
    Toolbar allOrderToolbar;
    @BindView(R.id.allOrder_viewPage)
    ViewPager allOrderViewPage;
    @BindView(R.id.allOrder_tab)
    TabLayout allOrderTab;

    //标题
    private List<String> titles;
    private List<Fragment> fragments;
    private ViewPagerAdapter pagerAdapter;
    private String userAccount;

    ShipmentsFragment shipmentsFragment;

    TakeGoodsFragment takeGoodsFragment;

    ConfirmFragment confirmFragment;

    AllFragment allFragment;

    ObligationFragment obligationFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        allOrderToolbar.setTitle("");
        allOrderToolbar.setNavigationIcon(R.mipmap.ic_back);
        allOrderToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(OrderActivity.this, WalletActivity.class));
                finish();
            }
        });

        initView();


    }


    private void initView() {
        titles = new ArrayList<>();
        fragments = new ArrayList<>();
        allFragment = new AllFragment();
        titles.add("全部");
        fragments.add(allFragment);
        obligationFragment = new ObligationFragment();
        titles.add("待付款");
        fragments.add(obligationFragment);
        shipmentsFragment = new ShipmentsFragment();
        titles.add("待发货");
        fragments.add(shipmentsFragment);
        takeGoodsFragment = new TakeGoodsFragment();
        titles.add("待收货");
        fragments.add(takeGoodsFragment);
        confirmFragment = new ConfirmFragment();
        titles.add("已确定");
        fragments.add(confirmFragment);

        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), this, titles, fragments);
        allOrderViewPage.setAdapter(pagerAdapter);
        allOrderTab.setupWithViewPager(allOrderViewPage);
        allOrderTab.setTabsFromPagerAdapter(pagerAdapter);
        allOrderTab.setTabTextColors(Color.WHITE, Color.WHITE);
        allOrderViewPage.setOffscreenPageLimit(1);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.allorder_layout;
    }

    @Override
    protected void setOnClick() {

    }

    @Override
    public void onClick(View v) {

    }
//    @Override
//    public void onStart() {
//        super.onStart();
//        allOrderTab.post(new Runnable() {
//            @Override
//            public void run() {
//                setIndicator(allOrderTab, 50, 50);
//            }
//        });
//    }


//    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
//        Class<?> tabLayout = tabs.getClass();
//        Field tabStrip = null;
//        try {
//            tabStrip = tabLayout.getDeclaredField("mTabStrip");
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        }
//        tabStrip.setAccessible(true);
//        LinearLayout llTab = null;
//        try {
//            llTab = (LinearLayout) tabStrip.get(tabs);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
//        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
//        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
//        for (int i = 0; i < llTab.getChildCount(); i++) {
//            View child = llTab.getChildAt(i);
//            child.setPadding(0, 0, 0, 0);
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
//            params.leftMargin = left;
//            params.rightMargin = right;
//            child.setLayoutParams(params);
//            child.invalidate();
//        }
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        startActivity(new Intent(this, WalletActivity.class));
        finish();
        return super.onKeyDown(keyCode, event);
    }

}
